package com.example.prom.qianduoduo.y2023;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LayeredRandomization {
    public static void main(String[] args) {
        // 创建包含120个受试者的列表
        List<Integer> participants = new ArrayList<>();
        for (int i = 1; i <= 120; i++) {
            participants.add(i);
        }

        // 将受试者列表随机排序
        Collections.shuffle(participants);

        // 创建30个小组
        List<List<Integer>> groups = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            groups.add(new ArrayList<>());
        }

        // 将受试者分配到小组中
        int groupIndex = 0;
        for (int participant : participants) {
            List<Integer> currentGroup = groups.get(groupIndex);
            currentGroup.add(participant);

            // 每个小组的第3个和第4个受试者为类型B，前面为类型A
            if (currentGroup.size() > 2) {
                int bCount = currentGroup.subList(2, currentGroup.size()).stream().mapToInt(i -> i % 2).sum();
                int aCount = currentGroup.size() - bCount;

                if (bCount > aCount) {
                    groupIndex++;
                }
            }

            // 到达最后一个小组后重新开始
            if (groupIndex == 30) {
                groupIndex = 0;
            }
        }

        // 打印表格
        System.out.println(String.format("%-10s%-10s%-10s%-10s", "Group", "Person", "Layer", "Participant"));
        System.out.println("--------------------------------------------");
        List<Integer> sortedParticipants = new ArrayList<>(participants);
        Collections.sort(sortedParticipants);
        for (int participant : sortedParticipants) {
            for (int i = 0; i < groups.size(); i++) {
                List<Integer> currentGroup = groups.get(i);
                if (currentGroup.contains(participant)) {
                    int layer = currentGroup.indexOf(participant) >= 2 ? 2 : 1;
                    System.out.println(String.format("%-10d%-10d%-10c%-10d", (i + 1), currentGroup.indexOf(participant) + 1, (layer == 1 ? 'A' : 'B'), participant));
                    break;
                }
            }
        }
    }
}
